capture log close
clear all
set more off
pause off
# delimit;

local output "";
local log "";
local data "";

/* 
Create Table A2
*/

foreach unit in geo ein { ;

	/* Prep: get stayer delta for each location */
	use "`data'\\`unit'_avg.dta", clear;
	qui drop if `unit' == . | `unit' == 0 ;
	qui drop mean_retire_age claim_age_age ;
	foreach stub in retire claim { ;
		foreach age in 65 66 { ;
			rename mean_`stub'_age_`age' `stub'_`age' ;
		} ;
	} ;
	qui reshape wide retire* claim*, i(`unit'_60) j(new_fra) ;
	
	foreach stub in retire claim { ;
		foreach age in 65 66 { ;
			rename `stub'_`age'1 dest_`stub'_`age'_post ;
			rename `stub'_`age'0 dest_`stub'_`age'_pre ;
			qui gen delt_dest_`stub'_`age' = dest_`stub'_`age'_post - dest_`stub'_`age'_pre ;
		} ;
	} ;
	
	rename `unit'_60 dest_loc ;
	tempfile stayerdelta ;
	save `stayerdelta' ;
	
	/* Prep: Movers file */
	use "`data'\\`unit'_movers_wearn.dta", clear;
	rename retire1 retire_age ;
	foreach age in 65 66 { ;
		gen byte retire_`age' = (retire_age == `age') if retire_age != . ;
	} ;
	foreach age in 65 66 { ;
		gen byte claim_`age' = (claim_age >= `age' & claim_age < `age'+1) if claim_age != . ;
	} ;

	replace orgn_loc = . if orgn_loc == 0 ;
	replace dest_loc = . if dest_loc == 0 ;
	gen dob_yy = year(dob) ;
	
	/* Keep only the "post" cohorts */
	keep if dob_yy >= 1943 & dob_yy != . ;

	/* Residualize lifetime earnings by origin */
	qui gen dummy = 1 ;
	qui areg n_earnavg25to55 dummy, absorb(orgn_loc) ;
	qui predict temp_n_earnavg25to55, xbd ;
	qui gen resid_n_earnavg25to55 = n_earnavg25to55 - temp_n_earnavg25to55 ;
	qui drop temp* ;

	/* Merge in stayer delta */
	qui merge m:1 dest_loc using `stayerdelta', keepusing(delt* dest*) ;
	qui drop if _merge == 2 ;
	qui drop _merge ;
	
	/* Table A2, Panel B: Correlation between lifetime earnings and stayer delta before and after residualizing by origin */
	qui collapse n_earnavg25to55 resid_n_earnavg25to55 delt*, by(dest_loc) ;
	foreach stub in retire claim { ;
		foreach age in 65 66 { ;
			di "**** Table A2, Panel B ****" ;
			corr n_earnavg25to55 delt_dest_`stub'_`age' ;
			pwcorr n_earnavg25to55 delt_dest_`stub'_`age', sig ;
			reg n_earnavg25to55 delt_dest_`stub'_`age' ;
			
			corr resid_n_earnavg25to55 delt_dest_`stub'_`age' ;
			pwcorr resid_n_earnavg25to55 delt_dest_`stub'_`age', sig ;
			reg resid_n_earnavg25to55 delt_dest_`stub'_`age' ;
		} ;
	} ;
	
	/* Table A2, Panel A: distribution of stayer delta */
	keep dest_loc ;
	rename dest_loc `unit'_60 ;
	tempfile units_with_movers ;
	save `units_with_movers' ;
	
	use "`data'\\`unit'_avg.dta", clear;
	qui drop if `unit' == . | `unit' == 0 ;
	qui drop mean_retire_age claim_age_age ;
	foreach stub in retire claim { ;
		foreach age in 65 66 { ;
			rename mean_`stub'_age_`age' `stub'_`age' ;
		} ;
	} ;
	qui reshape wide retire* claim*, i(`unit'_60) j(new_fra) ;
	
	merge 1:1 `unit'_60 using `units_with_movers' ;
	keep if _merge == 3 ;
	drop _merge ;
	
	foreach stub in retire claim { ;
		foreach age in 65 66 { ;
			rename `stub'_`age'1 dest_`stub'_`age'_post ;
			rename `stub'_`age'0 dest_`stub'_`age'_pre ;
			qui gen delt_dest_`stub'_`age' = dest_`stub'_`age'_post - dest_`stub'_`age'_pre ;
			di "**** Table A2, Panel A ****" ;
			summ delt_dest_`stub'_`age', det ;
		} ;
	} ;
	
} ;